#include <xp/bas/locale.h>
#include <xp/bas/string.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>

int xp_main ()
{
	xp_wchar_t buf[50];
	xp_size_t n;

	xp_setlocale ();

	{
		const xp_mchar_t* mstr = XP_MTEXT("abcdefg\xA4**");
		xp_wchar_t bufx[100];
		xp_size_t mstrlen = strlen(mstr);
		unsigned int i;

		printf ("---------------\n");
		for (i = 0; i < 10 && i < xp_countof(bufx); i++) {
			xp_size_t bufsz = i;	
			n = xp_mcsntowcs (mstr, mstrlen, bufx, &bufsz);
			printf ("buf[%u] ", i);

			if (n == -1) printf ("** conversion error **\n");
			else { 
				printf ("%lu/%lu bytes  %lu/%lu characters [%ls]\n", 
					n, mstrlen, bufsz, xp_mcsnwclen(mstr, mstrlen), bufx);
			}
		}
	}

	{
		const xp_mchar_t* mstr = XP_MTEXT("\xEC\x9C\xA0\xEB\x8B\x88\xEC\xBD\x94\xEB\x93\x9C");
		xp_wchar_t bufx[100];
		xp_size_t mstrlen = strlen(mstr);
		unsigned int i;

		printf ("---------------\n");
		for (i = 0; i < 10 && i + 1 < xp_countof(bufx); i++) {
			xp_size_t bufsz = i;	
			n = xp_mcsntowcs (mstr, mstrlen, bufx, &bufsz);
			printf ("buf[%u] ", i);

			if (n == -1) printf ("** conversion error **\n");
			else { 
				bufx[bufsz] = XP_WCHAR('\0');
				printf ("%lu/%lu bytes  %lu/%lu characters [%ls]\n", 
					n, mstrlen, bufsz, xp_mcsnwclen(mstr, mstrlen), bufx);
			}
		}
	}

	return 0;

}

